<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
   
        public function getIndex(Request $request)
        { 
          $id=1;
          //获取购物车数据 购车表和商品表联查获取数据
         $carts = DB::table('shop_cart')
                     ->leftJoin('shop_goods', 'shop_goods.gid', '=', 'shop_cart.gid')
                     ->get();
          // dd($carts);
          $sum = 0;
          
          $numarray=[];
          $gidarray=[];
          foreach ($carts as $k => $v) {
             $sum += ($v->price * $v->num);
             //获取所有商品的数量和id 
             array_push($numarray,$v->num);
             // return $numarray;
             array_push($gidarray,$v->gid);
            
          }
            //将数组转换为字符串
            $goodsnum = implode(',',$numarray);
            $goodsgid = implode(',',$gidarray);
          
          
            // dd($goodsnum);
          $add = DB::table('shop_address')->where('id',$id)->get();
          // dd($add);           
          return view('Home.Order.index',['carts'=>$carts,'add'=>$add,'goodsgid'=>$goodsgid,'goodsnum'=>$goodsnum,'sum'=>$sum]);
          
        }

        public function postInsert(Request $request)
        {
          // dd($request->all());
          //获取数据
          $data=$request->only(['aid','id','pay','ormb']);
          $data['code']=time()+rand(11111,99999);

          //订单添加
          $oid=DB::table('shop_orders')->insertGetId($data);
          $id=$data['id'];
          // dd($id);
          //获取数据
          $a=$request->only(['goodsnum','goodsgid']);
          $b=[];
          $c=[];
          $b=$a['goodsnum'];
          $c=$a['goodsgid'];
          //将字符串形式转为数组
          $num=explode(',',$b);
          $gid=explode(',',$c);
          //合成二维数组
          $goods=array('num'=>$num,'gid'=>$gid);
         // dd($goods);

           $data=[];
         foreach ($goods['gid'] as $k => $v){
            $tmp['gid']=$v;
            $tmp['num']=$goods['num'][$k];
            $tmp['oid']=$oid;
            $tmp['id']=$id;
            $data[]=$tmp;
         }
         //将数据添加到数据库
         $res = DB::table('shop_detail')->insert($data);
         $cid=[];
         $orders=DB::table('shop_detail')->where('oid',$oid)->get();
         $cart=DB::table('shop_cart')->get();
         foreach ($orders as $k => $v) {
            foreach ($cart as $kk => $vv) {
               if($v->id == $vv->id && $v->gid == $vv->gid && $v->num == $vv->num){
                 $cid[]=$vv->cid;
                 DB::table('shop_cart')->where('cid', '=', $cid)->delete();
               }
               
            }
          
         }
         foreach ($cid as $k => $v) {
           $res=DB::table('shop_cart')->where('cid', '=', $v)->delete();
         }
         // dd($res);
         //根据订单id查询订单号
         // $code = DB::table('shop_orders')->where('oid',$oid)->value('code');

         $orders = DB::table('shop_orders')
                     ->leftJoin('shop_address', 'shop_orders.aid', '=', 'shop_address.aid')
                     ->get();
        
         // dd($orders);
         return view('home.order.edit',['orders'=>$orders]);
        } 
        
        public function getDelete(Request $request)
        {
           $oid = $request->input('oid');
           // dd($oid);
           $res = DB::delete("delete from shop_orders where oid=".$oid);
           // dd($res);
           if($res){
               $orders = DB::table('shop_orders')
                                    ->leftJoin('shop_address', 'shop_orders.aid', '=', 'shop_address.aid')
                                    ->get();
               return view('home.order.edit',['orders'=>$orders]);
           }else{
               return back();
           }
        }     
}
